/*
 * CookingInstructionView.java
 *
 * Created on April 19, 2007, 12:52 AM
 */

package com.floreantpos.ui.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.DefaultListModel;

import com.floreantpos.model.CookingInstruction;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketCookingInstruction;
import com.floreantpos.ui.dialog.POSDialog;
import com.floreantpos.ui.dialog.SelectCookongInstructionDialog;

/**
 *
 * @author  bipu
 */
public class CookingInstructionView extends POSDialog implements ActionListener {
    
	Ticket ticket;
	DefaultListModel model;
	
    /** Creates new form CookingInstructionView */
    public CookingInstructionView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    
        initComponents();
        
        model = new DefaultListModel();
        listCookingInstructions.setModel(model);
        
		btnAdd.addActionListener(this);
        btnClose.addActionListener(this);
        btnRemove.addActionListener(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnAdd = new com.floreantpos.swing.PosButton();
        btnRemove = new com.floreantpos.swing.PosButton();
        btnClose = new com.floreantpos.swing.PosButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listCookingInstructions = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btnAdd.setText(com.floreantpos.POSConstants.ADD);
        btnAdd.setPreferredSize(new java.awt.Dimension(120, 50));
        jPanel1.add(btnAdd);

        btnRemove.setText(com.floreantpos.POSConstants.REMOVE);
        btnRemove.setPreferredSize(new java.awt.Dimension(120, 50));
        jPanel1.add(btnRemove);

        btnClose.setText(com.floreantpos.POSConstants.CLOSE);
        btnClose.setPreferredSize(new java.awt.Dimension(120, 50));
        jPanel1.add(btnClose);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel2.setLayout(new java.awt.BorderLayout());

        listCookingInstructions.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listCookingInstructions);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CookingInstructionView dialog = new CookingInstructionView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.floreantpos.swing.PosButton btnAdd;
    private com.floreantpos.swing.PosButton btnClose;
    private com.floreantpos.swing.PosButton btnRemove;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listCookingInstructions;
    // End of variables declaration//GEN-END:variables
    
    public void setTicket(Ticket ticket) {
		this.ticket = ticket;
		
		if(ticket == null) {
			return;
		}
		
		Set<TicketCookingInstruction> cookingInstructions = ticket.getCookingInstructions();
		if(cookingInstructions == null) {
			return;
		}
		
		for (TicketCookingInstruction instruction : cookingInstructions) {
			model.addElement(instruction);
		}
	}

    public void actionPerformed(ActionEvent e) {
    	String actionCommand = e.getActionCommand();
    	if(com.floreantpos.POSConstants.ADD.equals(actionCommand)) {
    		
			SelectCookongInstructionDialog dialog = new SelectCookongInstructionDialog(this, com.floreantpos.POSConstants.SELECT_COOKING_INSTRUCTION, true);
			dialog.pack();
			dialog.open();
			
			if(dialog.isCanceled()) {
				return;
			}
			
    		CookingInstruction cookingInstruction = (CookingInstruction) dialog.getCookingInstruction();
    		TicketCookingInstruction ticketCookingInstruction = new TicketCookingInstruction();
    		ticketCookingInstruction.setDescription(cookingInstruction.getDescription());
    		ticket.addCookingInstruction(ticketCookingInstruction);
    		model.addElement(ticketCookingInstruction);
    	}
    	if(com.floreantpos.POSConstants.REMOVE.equals(actionCommand)) {
    		int selectedIndex = listCookingInstructions.getSelectedIndex();
    		if(selectedIndex < 0) {
    			return;
    		}
    		TicketCookingInstruction ticketCookingInstruction = (TicketCookingInstruction) model.get(selectedIndex);
    		ticket.removeCookingInstruction(ticketCookingInstruction);
			model.remove(selectedIndex);
    	}
    	if(com.floreantpos.POSConstants.CLOSE.equals(actionCommand)) {
    		setCanceled(false);
    		dispose();
    	}
    		
	}
    
}
